package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.Company;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CompanyMapper {

    @Select("<script>" +
            "SELECT  \n" +
            " temp.COMPANY_ID companyId,  \n" +
            " temp.COMPANY_CODE companyCode,  \n" +
            " temp.COMPANY_NAME companyName,\n" +
            " temp.COMPANY_ARG companyArg\n" +
            "FROM (   \n" +
            "\tSELECT  \n" +
            "\t\tROWNUM rowno,  \n" +
            "\t\tt.*  \n" +
            "\tFROM T_DEVICE_COMPANY t\n" +
            "\tWHERE (t.COMPANY_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.COMPANY_CODE LIKE CONCAT(CONCAT('%', #{query}),'%')) \n" +
            "\tAND ROWNUM &lt;= #{pagesize}  ORDER BY t.COMPANY_ID\n" +
            ") temp WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<Company> findCompanyList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.COMPANY_ID) FROM T_DEVICE_COMPANY a WHERE a.COMPANY_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.COMPANY_CODE LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countCompanyList(String query);

    @Insert("<script>" +
            "INSERT INTO T_DEVICE_COMPANY \n" +
            "(COMPANY_ID,COMPANY_CODE,COMPANY_NAME" +
            "<if test=\" companyArg != null\">"+
            ",COMPANY_ARG" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{companyId},#{companyCode},#{companyName}" +
            "<if test=\" companyArg != null\">"+
            ",#{companyArg}" +
            "</if>"+
            ")" +
            "</script>")
    void addCompanyInfo(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.COMPANY_ID companyId,  \n" +
            " temp.COMPANY_CODE companyCode,  \n" +
            " temp.COMPANY_NAME companyName,\n" +
            " temp.COMPANY_ARG companyArg\n" +
            "FROM T_DEVICE_COMPANY temp\n" +
            "\tWHERE temp.COMPANY_ID = #{params} OR temp.COMPANY_CODE = #{params}\n" +
            "</script>")
    Company findCompanyInfoByIdOrCode(String params);

    @Update("<script>" +
            "UPDATE T_DEVICE_COMPANY SET \n" +
            "\tCOMPANY_CODE = #{companyCode},\n" +
            "\tCOMPANY_NAME = #{companyName}\n" +
            "<if test=\" companyArg != null\">"+
            "\t,COMPANY_ARG = #{companyArg}\n" +
            "</if>"+
            "WHERE COMPANY_ID = #{companyId}" +
            "</script>")
    void updateCompanyInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_DEVICE_COMPANY WHERE COMPANY_ID = #{companyId}</script>")
    void removeCompanyInfo(String companyId);
}
